package dream.dpx.pay.controller;

import com.github.pagehelper.PageHelper;
import dream.dpx.pay.domain.Config;
import dream.dpx.pay.domain.Orders;
import dream.dpx.pay.mapper.ConfigMapper;
import dream.dpx.pay.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.frame.context.Constant;
import zhibi.frame.context.LogKey;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;
import zhibi.frame.springmvc.base.AbstractController;

import java.util.List;

@Controller
@RequestMapping("sghluo")
public class AdminController extends AbstractController {
    @Autowired
    private ConfigMapper configMapper;
    @Autowired
    private OrdersMapper ordersMapper;


    @GetMapping("login")
    public String login() {
        return "admin/login";
    }

    @PostMapping("login")
    public String login(String username, String password) {
        if ("sghluo".equals(username) && "luo520859".equals(password)) {

        } else {
            request.setAttribute("errorMsg", "用户名或密码错误");
            return "error";
        }
        logger.info("【 登 录 】 用户:{}  ", username);
        session.setAttribute(Constant.SESSION.ADMIN, username);
        session.setAttribute(LogKey.DIAGNOSTIC_UID.name(), username);
        return redirect("index");
    }

    @RequestMapping("index")
    public String index(Model model, Page page, String name, String status) {
        page.setPageSize(50);
        Config config = configMapper.selectByPrimaryKey(1);
        model.addAttribute(config);

        Example example = Example.getInstance()
                .addParam("name", name, ExampleType.Operation.LIKE)
                .addParam("status", status, ExampleType.Operation.LIKE)
                .addOrder("addtime", ExampleType.OrderType.DESC);

        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Orders> ordersList = ordersMapper.selectByExample(example);
        model.addAttribute("ordersList", ordersList);
        model.addAttribute("pageNum", page.getPageNum());
        return "admin/index";
    }

    @RequestMapping("setMoney")
    public String setMoney(Double money) {
        Config config = configMapper.selectByPrimaryKey(1);
        config.setValue(money);
        configMapper.updateByPrimaryKeySelective(config);
        return refresh();
    }
}
